package com.fyh.mapper;

import com.fyh.entity.job.DeliveryRecord;
import com.fyh.entity.job.JobPosition;
import com.fyh.entity.job.JobPositionRequest;
import com.fyh.entity.job.SimpleJobPosition;
import com.fyh.entity.user.SimpleCandidate;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface JobMapper {

    /**
     * 获取所有行业id
     *
     * @return
     */
    List<Long> selectIndustryIdList();

    /**
     * 根据id获取行业名称
     *
     * @param id
     * @return
     */
    String selectIndustryNameById(Long id);

    /**
     * 根据行业id获取技能标签名称列表
     *
     * @param id
     * @return
     */
    List<String> selectTagNameListByIndustryId(Long id);

    /**
     * 根据 employerId 获取职位列表
     *
     * @param employerId
     * @return
     */
    List<SimpleJobPosition> selectSimpleJobListByEmployerId(Long employerId);

    /**
     * 根据职位id获取技能标签名称列表
     *
     * @param positionId
     * @return
     */
    List<String> selectTagNameListByPositionId(Long positionId);

    /**
     * 根据职位id获取职位详情
     *
     * @param positionId
     * @return
     */
    @Select("select * from job_position where position_id = #{positionId}")
    JobPosition selectJobPositionById(Long positionId);

    /**
     * 更新职位信息
     *
     * @param jobPosition
     */
    void updateJobPosition(JobPosition jobPosition);

    /**
     * 根据职位id删除职位技能标签
     *
     * @param positionId
     */
    @Delete("delete from job_position_skill where position_id = #{positionId}")
    void deleteTagByPositionId(Long positionId);

    /**
     * 插入职位技能标签
     *
     * @param positionId
     * @param tagName
     */
    void insertTag(@Param("positionId") Long positionId, @Param("tagName") String tagName);

    /**
     * 根据职位名称和职位类型获取职位列表
     *
     * @param jobPositionRequest
     * @return
     */
    List<SimpleJobPosition> getSimpleJobListByNameAndTypeAndTag(JobPositionRequest jobPositionRequest);

    /**
     * 插入职位信息
     *
     * @param jobPosition
     */
    void insertJobPosition(JobPosition jobPosition);

    /**
     * 删除职位信息
     *
     * @param positionId
     */
    @Delete("delete from job_position where position_id = #{positionId}")
    void deleteJobPosition(Long positionId);

    /**
     * 更新职位状态
     *
     * @param positionId
     * @param status
     */
    @Update("update job_position set status = #{status} where position_id = #{positionId}")
    void updateJobStatus(@Param("status") Integer status, @Param("positionId") Long positionId);

    /**
     * 获取职位是否投递
     *
     * @param positionId
     * @param userId
     * @return
     */
    @Select("select exists(select 1 from delivery_record where position_id = #{positionId} and user_id = #{userId})")
    Boolean getDeliveryExist(@Param("positionId") Long positionId, @Param("userId") Long userId);

    /**
     * 职位投递
     *
     * @param deliveryRecord
     */
    @Insert("insert into delivery_record (position_id, user_id, create_time, update_time) values (#{positionId}, #{userId}, #{createTime}, #{updateTime})")
    void insertDeliveryRecord(DeliveryRecord deliveryRecord);

    /**
     * 获取职位投递者ID列表及投递时间
     *
     * @param positionId
     * @param status
     * @return
     */
    List<DeliveryRecord> getSimpleDeliveryRecordListList(@Param("positionId") Long positionId, @Param("status") String status);

    /**
     * 更新职位投递者状态
     *
     * @param deliveryRecord
     */
    @Update("update delivery_record set status = #{status} where position_id = #{positionId} and user_id = #{userId}")
    void updateDeliveryStatus(DeliveryRecord deliveryRecord);

    /**
     * 获取收藏的职位基础信息
     *
     * @param positionId
     * @return
     */
    @Select("SELECT position_id, position_name, position_type, salary_range, status, province, update_time FROM job_position WHERE position_id = #{positionId}")
    SimpleJobPosition selectSimpleJobPositionById(Long positionId);

    /**
     * 通过职位Id获取employerId
     * @param positionId
     * @return
     */
    @Select("SELECT employer_id FROM job_position WHERE position_id = #{positionId}")
    Long getEmployerIdByPositionId(Long positionId);

    /**
     * 获取职位是否存在投递且未查看
     * @param positionId
     * @return
     */
    @Select("SELECT EXISTS(SELECT 1 FROM delivery_record WHERE position_id = #{positionId} AND status = 'not_viewed')")
    Boolean getDeliveryExistNotViewed(Long positionId);
}
